package com.rongzhe.house.presenter;

import android.content.Intent;

import com.rongzhe.house.entity.AppointmentTo;
import com.rongzhe.house.entity.voo.AppoiintmentInfo;
import com.rongzhe.house.internet.DataListener;
import com.rongzhe.house.manager.UserManager;
import com.rongzhe.house.ui.UiControlInterface;
import com.rongzhe.house.ui.activity.ListAppointmentActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huang on 2018/1/4.
 */

public class ListAppointmentPresenter extends BasePresenter {
    public ListAppointmentPresenter(UiControlInterface uiControlInterface) {
        super(uiControlInterface);
    }

    @Override
    public void start(Intent intent) {
        loadData(false);
    }


    public void loadData(final boolean finished) {
        UserManager.getInstance().listAppointment(new DataListener<List<AppoiintmentInfo>>() {
            @Override
            public void onSuccess(List<AppoiintmentInfo> data, String message) {
                if (data == null || data.size() == 0) {
                    ((ListAppointmentActivity)getUiControlInterface()).visibleEmptyView(true);
                } else {
                    ((ListAppointmentActivity)getUiControlInterface()).visibleEmptyView(false);
                    ((ListAppointmentActivity)getUiControlInterface())
                            .setData(filterData(finished, data));
                }
            }

            @Override
            public void onFailed(Throwable error, String message) {
                getUiControlInterface().showToast(message);
                ((ListAppointmentActivity)getUiControlInterface()).visibleEmptyView(true);
            }

            @Override
            public void onEnd() {

            }
        });
    }

    private List<AppoiintmentInfo> filterData(boolean finishedItem, List<AppoiintmentInfo> datas) {
        if (datas ==null || datas.size() < 1) {
            return new ArrayList<>();
        }
        List<AppoiintmentInfo> copyed = new ArrayList<>();
        for (AppoiintmentInfo data : datas) {
            if (finishedItem) {
                if (data.getState() == 2) {
                    copyed.add(data);
                }
            } else {
                if (data.getState() != 2) {
                    copyed.add(data);
                }
            }
        }
        return copyed;
    }
}
